﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace RapChieuPhim.GUI
{
    public partial class QLSuatChieu : UserControl
    {
        private int _MaRap = Modules.Login.user.NhanVien.Rap.MaRap;
        //private int _MaRap = 2;
        int _landau = 0;

        public QLSuatChieu()
        {
            InitializeComponent();
        }

        private void LoadData()
        {
            lookCa.Properties.DataSource = BUS.CaChieuBUS.GetAll();
            if (_landau==0)
            {
                lookCa.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("MaCaChieu", 50, "Ca"));
                lookCa.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("GioBD", 80, "Giờ bắt đầu"));
                lookCa.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("GioKT", 80, "Giờ kết thúc"));
            }
            lookCa.Properties.Columns[1].FormatType = DevExpress.Utils.FormatType.Custom;
            lookCa.Properties.Columns[1].FormatString = "t";
            lookCa.Properties.Columns[2].FormatType = DevExpress.Utils.FormatType.Custom;
            lookCa.Properties.Columns[2].FormatString = "t";
            lookCa.Properties.DisplayMember = "MaCaChieu";
            lookCa.Properties.ValueMember = "MaCaChieu";
            lookCa.Properties.PopupFormMinSize = new Size(210, 100);
            lookCa.ItemIndex = 0;

            lookPhong.Properties.DataSource = BUS.PhongChieuBUS.GetAllbyMaRap(_MaRap);
            if (_landau==0)
                lookPhong.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("MaPhong", 100, "Phòng"));
            lookPhong.Properties.DisplayMember = "MaPhong";
            lookPhong.Properties.ValueMember = "MaPhong";
            lookPhong.Properties.PopupFormMinSize = new Size(100, 100);
            lookPhong.ItemIndex = 0;

            gridControlMain.DataSource = BUS.SuatChieuBUS.GetAllbyMaRap(_MaRap);
            _landau++;
        }

        private void QLSuatChieu_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void navBarItem4_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            int _tempid = BUS.SuatChieuBUS.LayLastSuatChieu() + 1;
            if (XtraMessageBox.Show("Thêm suất chiếu này ?", null, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DTO.SuatChieuDTO _Suat = new DTO.SuatChieuDTO();
                _Suat.MaSuatChieu = _tempid;
                int MaCaChieu = int.Parse(lookCa.EditValue.ToString());
                int Phong = int.Parse(lookPhong.EditValue.ToString());
                if (MaCaChieu == 1)
                {
                    _Suat.GioBD = DateTime.Parse("09:00:00");
                    _Suat.GioKT = DateTime.Parse("11:59:59");
                }
                else if (MaCaChieu == 2)
                {
                    _Suat.GioBD = DateTime.Parse("12:00:00");
                    _Suat.GioKT = DateTime.Parse("16:59:59");
                }
                else if (MaCaChieu == 3)
                {
                    _Suat.GioBD = DateTime.Parse("17:00:00");
                    _Suat.GioKT = DateTime.Parse("18:59:59");
                }
                else if (MaCaChieu == 4)
                {
                    _Suat.GioBD = DateTime.Parse("19:00:00");
                    _Suat.GioKT = DateTime.Parse("20:59:59");
                }
                else if (MaCaChieu == 5)
                {
                    _Suat.GioBD = DateTime.Parse("21:00:00");
                    _Suat.GioKT = DateTime.Parse("23:59:59");
                }
                BUS.SuatChieuBUS.Insert(_Suat, MaCaChieu, Phong);
                LoadData();
                gridView1.FocusedRowHandle = gridView1.RowCount - 1;
                XtraMessageBox.Show("Thêm thành công !", null, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0 || gridView1.FocusedRowHandle >= gridView1.RowCount)
                return;
            int _Value = int.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colMa).ToString());
            DTO.SuatChieuDTO _SuatDTO = BUS.SuatChieuBUS.GetByID(_Value);
            BindingData(_SuatDTO);
        }

        private void BindingData(DTO.SuatChieuDTO _SuatDTO)
        {
            txtSuat.Text = _SuatDTO.MaSuatChieu.ToString();
            lookPhong.EditValue = _SuatDTO.Phong.MaPhong;
            lookCa.EditValue = _SuatDTO.CaChieu.MaCaChieu;
        }

        private void navBarItem5_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (XtraMessageBox.Show("Xóa suất chiếu này ?", null, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int _Value = int.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colMa).ToString());
                BUS.SuatChieuBUS.Delete(_Value);
                LoadData();
                XtraMessageBox.Show("Xóa thành công !", null, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void navBarItem6_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (XtraMessageBox.Show("Sửa suất chiếu này ?", null, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int _Value = int.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colMa).ToString());
                DTO.SuatChieuDTO _Suat = new DTO.SuatChieuDTO();
                _Suat.MaSuatChieu = _Value;
                int MaCaChieu = int.Parse(lookCa.EditValue.ToString());
                int Phong = int.Parse(lookPhong.EditValue.ToString());
                if (MaCaChieu == 1)
                {
                    _Suat.GioBD = DateTime.Parse("09:00:00");
                    _Suat.GioKT = DateTime.Parse("11:59:59");
                }
                else if (MaCaChieu == 2)
                {
                    _Suat.GioBD = DateTime.Parse("12:00:00");
                    _Suat.GioKT = DateTime.Parse("16:59:59");
                }
                else if (MaCaChieu == 3)
                {
                    _Suat.GioBD = DateTime.Parse("17:00:00");
                    _Suat.GioKT = DateTime.Parse("18:59:59");
                }
                else if (MaCaChieu == 4)
                {
                    _Suat.GioBD = DateTime.Parse("19:00:00");
                    _Suat.GioKT = DateTime.Parse("20:59:59");
                }
                else if (MaCaChieu == 5)
                {
                    _Suat.GioBD = DateTime.Parse("21:00:00");
                    _Suat.GioKT = DateTime.Parse("23:59:59");
                }
                BUS.SuatChieuBUS.Update(_Suat, MaCaChieu, Phong);
                int _temp = gridView1.FocusedRowHandle;
                LoadData();
                gridView1.FocusedRowHandle = _temp;
                XtraMessageBox.Show("Sửa thành công !", null, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void navBarItem7_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            int _temp = gridView1.FocusedRowHandle;
            LoadData();
            gridView1.FocusedRowHandle = _temp;
        }

        private void navBarItem2_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            gridControlMain.ShowPrintPreview();
        }

        private void navBarItem8_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            SaveFileDialog _dialog = new SaveFileDialog();
            _dialog.InitialDirectory = Application.StartupPath.ToString();
            _dialog.Title = "Xuất ra file pdf";
            _dialog.DefaultExt = "pdf";
            _dialog.Filter = "All files (*.*)|*.*";
            _dialog.FilterIndex = 0;
            _dialog.RestoreDirectory = true;
            if (_dialog.ShowDialog() == DialogResult.OK)
            {
                gridControlMain.ExportToPdf(_dialog.FileName.ToString());

                System.Diagnostics.Process _prc = new System.Diagnostics.Process();
                _prc.StartInfo.FileName = System.IO.Path.GetDirectoryName(_dialog.FileName);
                _prc.Start();
            }
        }
    }
}
